package com.example.springbootmybatis.controller;

import com.example.springbootmybatis.mapper.UserMapper;
import com.example.springbootmybatis.entity.User;
import com.example.springbootmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @title: UserController
 * @Author Tan
 * @Date: 2021/6/3 20:38
 * @Version 1.0
 */
// @RestController

@Controller
public class UserController {
    /*
     * @Description: 用户Mapper
     * @Date: 2021/6/5 18:01
     */
    private UserService userService;

    /*
     * @Description: 变量的依赖注入最好通过构造器或者Set方法注入
     * @Date: 2021/6/5 18:00
     */
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后，写入到response对象的body区，通常用来返回JSON数据或者是XML数据。
    @ResponseBody
    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") Integer id) {
        User user = userService.selectByPrimaryKey(id);
        return user;
    }
}

